# -*- coding: utf-8 -*-

# @File  : db.py
# @Author: Lomo
# @Site  : lomo.space
# @Date  : 2019-11-27
# @Desc  : DML


import pymysql

from config.db_config import DB_CONFIG


class DB(object):
    def __init__(self):
        self.host = DB_CONFIG.get('host')
        self.port = DB_CONFIG.get('port')
        self.user = DB_CONFIG.get('user')
        self.password = DB_CONFIG.get('password')
        self.db = DB_CONFIG.get('database')

    def __conn(self):
        return pymysql.connect(
            host=self.host,
            port=self.port,
            user=self.user,
            password=self.password,
            db=self.db
        )

    def __exec(self, sql):
        cursor = self.__conn().cursor()
        return cursor.execute(sql)

    def __close_conn(self):
        return self.__conn().close()

    @staticmethod
    def connect():
        return DB().__conn()

    @staticmethod
    def exec(sql):
        return DB().__exec(sql)

    @staticmethod
    def close():
        return DB().__close_conn()

    @classmethod
    def dict_cursor(cls):
        return cls.connect().cursor(pymysql.cursors.DictCursor)
